package com.open.spring.utils.validate;

import java.lang.reflect.Field;

import org.apache.commons.lang.StringUtils;

import com.open.spring.utils.validate.anno.RegexType;
import com.open.spring.utils.validate.anno.Regulation;

/**
 * 注解解析
 * 
 * @author chx
 */
public class ValidateUtils {

	private static Regulation regulation;

	public ValidateUtils() {
		super();
	}

	// 解析的入口
	public static void valid(Object object) throws Exception {
		// 获取object的类型
		Class<? extends Object> clazz = object.getClass();
		// 获取该类型声明的成员
		Field[] fields = clazz.getDeclaredFields();
		// 遍历属性
		for (Field field : fields) {
			// 对于private私有化的成员变量，通过setAccessible来修改器访问权限
			field.setAccessible(true);
			validate(field, object);
			// 重新设置会私有权限
			field.setAccessible(false);
		}
	}

	private static void validate(Field field, Object object) throws Exception {

		String description;
		Object value;

		// 获取对象的成员的注解信息
		regulation = field.getAnnotation(Regulation.class);
		value = field.get(object);
		if(null==regulation){
			return;
		}
		description = regulation.name().equals("") ? field.getName() : regulation.name();

		/************* 注解解析工作开始 ******************/
		if (regulation.notNull()) {
			if (value == null || StringUtils.isBlank(value.toString())) {
				throw new IllegalArgumentException(description + "不能为空");
			}
		}else{
			if(null==value){
				value="";
			}
		}
		if (regulation.maxLen() != -1 && value.toString().length() > regulation.maxLen()) {
			throw new IllegalArgumentException(description+"长度不能超过" + regulation.maxLen());
		}

		if (regulation.minLen() != -1 && value.toString().length() < regulation.minLen()) {
			throw new IllegalArgumentException(description+"长度不能小于" + regulation.minLen());
		}

		String validateStr = validateRegexType(regulation, value);
		if (!"".equals(validateStr)) {
			throw new IllegalArgumentException(validateStr);
		}
		if (!regulation.regStr().equals("")) {
			if (value.toString().matches(regulation.regStr())) {
				throw new IllegalArgumentException(description + "格式不正确");
			}
		}
		/************* 注解解析工作结束 ******************/
	}

	public static String validateRegexType(Regulation regulation, Object value) {
		if (regulation.regexType() != RegexType.NONE) {
			String description = regulation.name();
			switch (regulation.regexType()) {
			case NONE:
				break;
			case SPECIALCHAR:
				if (RegexUtils.hasSpecialChar(value.toString())) {
					return description + "不能含有特殊字符";
				}
				break;
			case CHINESE:
				if (RegexUtils.isChinese2(value.toString())) {
					return description + "不能含有中文字符";
				}
				break;
			case EMAIL:
				if (!RegexUtils.isEmail(value.toString())) {
					return description + "地址格式不正确";
				}
				break;
			case IP:
				if (!RegexUtils.isIp(value.toString())) {
					return description + "地址格式不正确";
				}
				break;
			case NUMBER:
				if (!RegexUtils.isNumber(value.toString())) {
					return description + "不是数字";
				}
				break;
			case PHONENUMBER:
				if (!RegexUtils.isPhoneNumber(value.toString())) {
					return description + "格式不正确";
				}
				break;
			default:
				break;
			}
		}
		return "";
	}
}
